package app.common.utils.history;

import app.common.utils.WdtUtils;
import app.common.utils.cjt.CJtUtils;
import app.common.utils.cjt.ao.ToOtherReceiveCreateAoDto;
import app.common.utils.cjt.ao.WmsStockPdQueryStockPdInDetailAo;
import app.common.utils.cjt.ao.WmsStockinOtherQueryWithDetailAo;
import app.common.utils.cjt.ao.WmsStockinTransferQueryWithDetailAo;
import app.temporary.Error;
import lombok.SneakyThrows;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * wdt ck
 * @author feizns
 * @since 2023/07/21
 */
public interface WdtToRK {

    /**
     * 错误
     */
    List<Error<ToOtherReceiveCreateAoDto>> errors = new ArrayList<>();

    /**
     * 成功数
     */
    AtomicInteger successCount = new AtomicInteger(0);

    /**
     * 发送订单号
     */
    List<String> sendOrderNos = new ArrayList<>();

    static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2023, 1, 1);
        LocalDate endDate = LocalDate.of(2023, 1, 25);
        WdtToRK.wdtToRk(startDate, endDate);
        System.out.println("传输完毕");
        System.out.println(sendOrderNos);
        System.out.println("共传递：" + successCount);
        System.out.println("错误列表：");
        System.out.println(errors);
    }

    /**
     * wdt其他入库转畅捷通其他出库
     * @param startDate 开始日期
     * @param endDate 结束日期
     */
    @SneakyThrows
    static void wdtToRk(LocalDate startDate, LocalDate endDate) {
        LocalDateTime startTime = startDate.atStartOfDay();
        LocalDateTime endTime = endDate.atStartOfDay();
        WmsStockinOtherQueryWithDetailAo ao = new WmsStockinOtherQueryWithDetailAo(startTime, endTime);
        //其他入库单创建
        WdtUtils.wmsStockinOtherQueryWithDetail(ao, order -> {
            if ( order.getOperator_name() != null && !order.getOperator_name().trim().isEmpty() )
                execute(order);
        });
        //调拨入库单创建
        WdtUtils.wmsStockinTransferQueryWithDetailAo(new WmsStockinTransferQueryWithDetailAo(startTime, endTime), WdtToRK::execute);
//        //生产入库单
//        WdtUtils.wmsStockinProcessQueryWithDetail(new WmsStockinProcessQueryWithDetailAo(startTime, endTime), WdtToRK::execute);
        //盘点入库单
        WdtUtils.wmsStockPdQueryStockPdInDetail(new WmsStockPdQueryStockPdInDetailAo(startTime, endTime), WdtToRK::execute);
    }

    private static void execute(ToOtherReceiveCreateAoDto order) {
        try {
            CJtUtils.otherReceiveCreate(order.toOtherReceiveCreateAo());
            successCount.incrementAndGet();
        } catch (Exception e) {
            errors.add(new Error<>(order, e));
            e.printStackTrace();
        }
    }

}
